package com.dao.impl;

import com.dao.IStudentDAO;
import com.entity.PageData;
import com.entity.Student;
import com.util.BaseDAO;

import java.util.List;
import java.util.Map;

public class StudentDAOImpl implements IStudentDAO {
    @Override
    public int delete(String ids) {
        System.out.println("=======StudentDAOImpl delete===============");
        String sql = "delete from studentInfo where studentId in ("+ids +") ";
        return BaseDAO.executeUpdate(sql,null);
    }

    @Override
    public List<Map<String, Object>> listAll() {
        System.out.println("=======StudentDAOImpl listAll===============");
        String sql = "select s.studentId,s.name,s.sex,s.birthday,s.province," +
                "     c.classId,c.className " +
                "     from studentInfo s " +
                "     join classInfo c " +
                "     on s.classId = c.classId ";
        System.out.println(sql);
        return BaseDAO.executeQuery(sql,null);
    }

    @Override
    public int insert(Student student) {
        String sql="insert into studentInfo "+
                "   (name,sex,birthday,province,classId)"+
                "   values"+
                "   (?,?,?,?,?)";
        Object[] params={
                student.getName(),
                student.getSex(),
                student.getBirthday(),
                student.getProvince(),
                student.getClassId()
        };
        return BaseDAO.executeUpdate(sql,params);
    }

    @Override
    public int update(Student student) {
        String sql = "update studentInfo" +
                "     set name = ?," +
                "         sex = ?," +
                "         birthday = ?," +
                "         province  = ?," +
                "         classId = ? " +
                "     where studentId = ? ";
        Object[] params = {
                student.getName(),
                student.getSex(),
                student.getBirthday(),
                student.getProvince(),
                student.getClassId(),
                student.getStudentId()
        };
        return BaseDAO.executeUpdate(sql,params);
    }

    @Override
    public PageData queryByPage(Integer pageNo, Integer pageSize) {
        //获取总记录数
        String totalSql = "select count(*) from studentInfo ";
        //总记录数
        Integer totalCount = BaseDAO.getTotal(totalSql,null);
        //总页数
        Integer totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;

        //分页查询
        String pageSql = "select s.studentId,s.name,s.sex,s.birthday,s.province," +
                "     c.classId,c.className " +
                "     from studentInfo s " +
                "     join classInfo c " +
                "     on s.classId = c.classId " +
                "     order by s.studentId " +
                "     limit ?,? ";
        int start = (pageNo-1)*pageSize;
        Object[] params = {start,pageSize};
        List<Map<String,Object>> rows = BaseDAO.executeQuery(pageSql,params);

        //封装分页数据对象
        PageData pd = new PageData();
        pd.setPageNo(pageNo);
        pd.setPageSize(pageSize);
        pd.setTotalPage(totalPage);
        pd.setTotalCount(totalCount);
        pd.setData(rows);

        return pd;
    }

}
